package com.xiaohai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.common.R;
import com.xiaohai.projo.Class;
import com.xiaohai.projo.Major;
import com.xiaohai.projo.Student;
import com.xiaohai.projo.dto.StudentDto;
import com.xiaohai.service.ClassService;
import com.xiaohai.service.MajorService;
import com.xiaohai.service.StudentService;
import com.xiaohai.utils.ToPinyinUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaohai
 * @since 2024-01-22
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

    @Autowired
    private MajorService majorService;

    @Autowired
    private ClassService classService;


    @GetMapping("/list")
    public R<Page<StudentDto>> list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, String studentName, String majorName, String className, Integer identity, String year,String account) {
        Page<StudentDto> list = studentService.mylist(currentPage, pageSize, studentName, majorName, className, identity, year,account);
        return R.success(list, "获取数据成功");
    }

    @GetMapping
    public R<StudentDto> detail(String id) {
        StudentDto student = studentService.detail(id);
        if (Objects.isNull(student)) {
            return R.error("获取详情失败", 201);
        }
        return R.success(student, "获取详情成功");
    }

    @PostMapping
    public R<Objects> addAndUpdate(@RequestBody Student student) {
        Integer id = student.getId();
        Integer classId = student.getClassId();
        Integer total = classService.getById(classId).getTotal();
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(classId != null, Student::getClassId, classId);
        long count = studentService.count(studentLambdaQueryWrapper);
        if (id != null) { // 修改
            if (count > total) {
                return R.error("班级满员了，修改失败", 201);
            }
            Boolean b = studentService.myUpdate(student);
            if (b) {
                return R.success(null, "修改成功");
            }
            return R.error("修改失败", 201);

        } else {
            if (count > total) {
                return R.error("班级满员了，新增失败", 201);
            }
            String account = ToPinyinUtil.toPinyin(student.getName());
            student.setAccount(account);
            String s = DigestUtils.md5DigestAsHex("123456".getBytes());
            student.setPassword(s);
            student.setIdentity(2);
            boolean add = studentService.add(student);
            if (add) {
                return R.success(null, "新增成功");
            }
            return R.error("新增失败", 201);
        }
    }

    @DeleteMapping
    public R<Object> deleteById(Integer id) {
        log.info("id:{}", id);
        boolean b = studentService.delete(id);
        if (b) {
            return R.success(null, "删除成功");
        }
        return R.error("删除失败,该同学的分数存在不能够被删除", 201);
    }

    @PostMapping("/batchAdd")
    public R<Objects> batchAdd(@RequestBody List<Student> students) {
        log.info("students:{}", students);
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            Integer classId = student.getClassId();
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper.eq(classId != null, Student::getClassId, classId);
            long count = studentService.count(studentLambdaQueryWrapper);
            Integer total = classService.getById(classId).getTotal();
            if (count >= total) {
                return R.error("班级满员了，新增失败", 201);
            }
            student.setAccount(ToPinyinUtil.toPinyin(student.getName()));
            String s = DigestUtils.md5DigestAsHex("123456".getBytes());
            student.setPassword(s);
            student.setIdentity(2);
        }

        Boolean b = studentService.batchAdd(students);
        if (b) {
            return R.success(null, "新增成功");
        }
        return R.error("新增失败", 201);
    }

    @GetMapping("/majorList")
    public R<List<Major>> teacherList() {
        List<Major> list = majorService.list();
        if (Objects.isNull(list)) {
            return R.error("获取数据失败", 201);
        }
        return R.success(list, "获取数据成功");
    }


    @GetMapping("/classList")
    public R<List<Class>> classList() {
        List<Class> list = classService.list();
        if (Objects.isNull(list)) {
            return R.error("获取数据失败", 201);
        }
        return R.success(list, "获取数据成功");
    }
}
